//
//  MCChangePasswordTwoViewController.swift
//  StoreManage
//
//  Created by MC on 2018/8/29.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCChangePasswordTwoViewController: MCViewController {

    public var fatherVC : UIViewController?   // 用来做present模态跳转
    public var phoneNumber = ""               // 传过来的手机号
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        managerClosure()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Closure
    func managerClosure() { }
    
    // MARK: - Action
    @objc func confirmButtonClicked() {
        
        let phoneStr = phoneTextField.text ?? ""
        let oldStr = oldPasswordTextField.text ?? ""
        let newStr = newPasswordTextField.text ?? ""
        
        
        guard (phoneStr.count == 11 && oldStr.count != 0 && newStr.count != 0) else {
            MCAlterController_oneOptions(vc: self, message: "账号,或旧密码或新密码不正确", cancle: { })
            return
        }
        
        
        let para = [
            "userName"    : phoneStr,
            "oldPassword" : oldStr.MCMD5(),
            "newPassword" : newStr.MCMD5()
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_resetPassword, para: para as [String : AnyObject], vc: self,hudSuperView:self.view, isHiddenTabbar: true, success: { (response) in
            
            let dataDcit = response["data"] as? [String:Any] ?? [:]
            
            
            MCHUD_text(message: "修改密码成功", view: self.view, delay: 1)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                self.view.endEditing(true)
                let vc = MCChangeStoreViewController()
                vc.isLoginVCJump = true
                vc.userDict = dataDcit
                
                let nav = MCNavigationController.init(rootViewController: vc)
                self.fatherVC!.present(nav, animated: true, completion: nil)
            })
            
        }, dislike: { (response) in }) { (error) in }

    }
    
    // MARK: - Network
    func sendNetworking() {
        
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "修改密码"
        phoneTextField.text = phoneNumber
        
        
    }
    
    func initUI() {
        view.addSubview(phoneTextField)
        phoneTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(25)
            make.height.equalTo(40)
        }
        
        view.addSubview(oldPasswordTextField)
        oldPasswordTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(phoneTextField.snp.bottom).offset(10)
            make.height.equalTo(40)
        }
        
        view.addSubview(newPasswordTextField)
        newPasswordTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(oldPasswordTextField.snp.bottom).offset(10)
            make.height.equalTo(40)
        }

        
        view.addSubview(confirmButton)
        confirmButton.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.top.equalTo(newPasswordTextField.snp.bottom).offset(80)
            make.size.equalTo(CGSize.init(width: MCScreenWidth * 0.65, height: 40))
        }

    }
    
    // MARK: - Setter & Getter
    lazy var phoneTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.placeholder = "请输入手机号码"
        textField.font = MCFont(14)
        textField.keyBoardType = MCKeyBorardType.Number
        return textField
    }()
    
    lazy var oldPasswordTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.placeholder = "请输入旧密码"
        textField.font = MCFont(14)
        textField.keyBoardType = MCKeyBorardType.asciiCapable
        textField.isSecureTextEntry = true
        return textField
    }()
    
    lazy var newPasswordTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.placeholder = "请输入新的登录密码"
        textField.font = MCFont(14)
        textField.keyBoardType = MCKeyBorardType.asciiCapable
        textField.isSecureTextEntry = true

        return textField
    }()
    


    
    lazy var confirmButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.backgroundColor = UIColor.MCMain
        button.layer.cornerRadius = 4
        button.layer.masksToBounds = true
        button.setTitle("完成", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(confirmButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

}
